﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.WowAddonStudio.Lua.Support
{
    public static class EnumerableExtensions
    {
        /// <summary>
        /// Iterates through a collection and executes the action for each item.
        /// </summary>
        /// <typeparam name="T">The type of items in the collection.</typeparam>
        /// <param name="collection">The collection to iterate.</param>
        /// <param name="action">The action to execute.</param>
        public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
        {
            if (collection != null)
            {
                foreach (var item in collection)
                {
                    action(item);
                }
            }
        }

        public static string Concatenate<T>(this IEnumerable<T> collection, char delimiter)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");

            return collection.Concatenate(delimiter.ToString());
        }

        public static string Concatenate<T>(this IEnumerable<T> collection, string delimiter)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");

            var stringBuilder = new StringBuilder();

            foreach (var item in collection)
            {
                if (stringBuilder.Length > 0)
                    stringBuilder.Append(delimiter);

                stringBuilder.Append(item);
            }

            return stringBuilder.ToString();
        }
    }
}
